package controllers

import (
	"my-go-project/config"
	"my-go-project/model"
	"my-go-project/utils/token"
	"net/http"
	"os"
	"path/filepath"
	"strconv"

	"github.com/gin-gonic/gin"
)

type PassageResponse struct {
	AuthorID       int      `json:"AuthorID"`
	CommentNum     int      `json:"CommentNum"`
	IfSubscribe    int      `json:"ifSubscribe"`
	IfLiked        bool     `json:"ifLiked"`
	LikeNum        int      `json:"LikeNum"`
	PassageArea    *string  `json:"PassageArea,omitempty"`
	PassageContent string   `json:"PassageContent"`
	PassageID      int      `json:"PassageID"`
	PassagePicList []string `json:"PassagePicList"`
	PassageTime    string   `json:"PassageTime"`
	PassageTitle   string   `json:"PassageTitle"`
	PassageType    string   `json:"PassageType"`
	ProfilePhoto   string   `json:"ProfilePhoto"`
	UName          string   `json:"UName"`
}

type PassagePicResponse struct {
	PassagePicList []string `json:"PassagePicList"`
}

func GetPassage(ctx *gin.Context) {
	var passageResponse PassageResponse

	pid, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	passage, err := model.GetPassageContent(pid)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	passageResponse.AuthorID = passage.AuthorID
	passageResponse.CommentNum = passage.CommentNum
	passageResponse.LikeNum = passage.LikeNum
	passageResponse.PassageArea = &passage.PassageArea
	passageResponse.PassageContent = passage.PassageContent
	passageResponse.PassageID = passage.PassageID
	passageResponse.PassageTime = passage.PassageTime
	passageResponse.PassageTitle = passage.PassageTitle
	passageResponse.PassageType = passage.PassageType

	userMessage, err := model.GetUserMessage(passage.AuthorID)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	passageResponse.UName = userMessage.UName
	passageResponse.ProfilePhoto = userMessage.ProfilePhoto

	uid, err := token.ExtractTokenID(ctx)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	ifLiked, err := model.IfLiked(pid, int(uid))
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	passageResponse.IfLiked = ifLiked

	var iii int
	if int(uid) == passage.AuthorID {
		iii = 0
	} else {
		ifFollowed, err := model.IfFollowed(int(uid), passage.AuthorID)
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"error": err.Error(),
			})
			return
		}
		if ifFollowed {
			iii = 1
		} else {
			iii = 2
		}
	}
	passageResponse.IfSubscribe = iii

	// 获取文章图片路径
	err = filepath.Walk(config.AppConfig.File.AbsolutePath+passage.PassagePic, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() { // 不处理文件夹
			passageResponse.PassagePicList = append(passageResponse.PassagePicList, passage.PassagePic+filepath.Base(path))
		}
		return nil
	})
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, passageResponse)
}

func GetPassagePic(ctx *gin.Context) {
	var passagePicResponse PassagePicResponse
	var passagePic []string

	pid, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	passagePath, err := model.GetPassagePic(pid)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	err = filepath.Walk(config.AppConfig.File.AbsolutePath+passagePath.PassagePic, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if !info.IsDir() { // 不处理文件夹
			passagePic = append(passagePic, passagePath.PassagePic+filepath.Base(path))
		}
		return nil
	})
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	passagePicResponse.PassagePicList = passagePic
	ctx.JSON(http.StatusOK, passagePicResponse)
}
